/*Copyright (C) 2009 Careil Baptiste

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Init.h"
#include "Colors.h"
#include <curses.h>
#include <assert.h>

int  DLL_IMPEXP CSLP_InitCurses(int flags, int delay)
{
  if (initscr() == NULL)
    {
      return 1;
    }
  if (delay != 0)
    {
      halfdelay(delay);
    }
  if (flags & CSLP_INIT_NO_ECHO)
    {
      noecho();
    }
  else
    {
      echo();
    }
  if (flags & CSLP_INIT_RAW)
    {
      raw();
      if (flags & CSLP_INIT_CBREAK)
	{
	  flags &= ~CSLP_INIT_CBREAK;
	}
    }
  if (flags & CSLP_INIT_CBREAK)
    {
      cbreak();
    }
  if (flags & CSLP_INIT_KEYPAD)
    {
      keypad(stdscr, TRUE);
    }
  if (flags & CSLP_INIT_COLORS)
    {
      start_color();
    }
  return (0);
}

int  DLL_IMPEXP CSLP_InitMainColorPairs()
{
  int ret_val = 0;

  if (has_colors() != TRUE)
  {
    ret_val = -1;
  }
  else
  {
    if (COLOR_PAIRS < 64)
    {
      ret_val = -2;
    }
    else
    {
      int fg, bg;

      for (bg = 0; bg <= 7; bg++)
	for (fg = 0; fg <= 7; fg++)
	  init_pair(CSLP_GetPair(fg, bg), fg, bg);
      attron(CSLP_GetPairAttr(COLOR_WHITE, COLOR_BLACK));
    }
  }
  return ret_val;
}
